package org.jsbd.boss.action;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.DouyingAddConfig;
import org.jsbd.boss.service.IDouyingAddConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 抖音配置
 * @author lhz
 * @date:   2018年8月22日 上午9:06:03
 */
@Controller
@RequestMapping("/douying")
public class DouYingConfigController {
	public static final Logger LOGGER = LoggerFactory.getLogger(DouYingConfigController.class);

	@Autowired
	private IDouyingAddConfigService douyingAddConfigService;
	
	@RequestMapping(value = "/add/config", method = RequestMethod.GET)
	public String addConfig(Model model) {
		return "douying/addConfig";
	}
	
	@RequestMapping(value = "getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page,  DouyingAddConfig douyingAddConfig) throws Exception {
		return douyingAddConfigService.getListPage(page, douyingAddConfig);
	}
	

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(DouyingAddConfig douyingAddConfig) {
		douyingAddConfig.setDeleteFlag(0);
		douyingAddConfigService.save(douyingAddConfig);
		return ResponseMapUtils.responseSuccess();
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(DouyingAddConfig douyingAddConfig) {
		if (douyingAddConfig.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		douyingAddConfigService.update(douyingAddConfig);
		return ResponseMapUtils.responseSuccess();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		if (ids.length == 0) {
			ResponseUtils.responseFailure(response);
		}
		try {
			List<Integer> list = Arrays.asList(ids);
			douyingAddConfigService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}
	
	@RequestMapping(value = "/startConfig", method = RequestMethod.POST)
	public void startConfig(Integer[] ids, HttpServletResponse response) {
		if (ids.length == 0) {
			ResponseUtils.responseFailure(response);
		}
		try {
			List<Integer> list = Arrays.asList(ids);
			douyingAddConfigService.startConfig(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}
	
}
